package top.hencter.weibo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;

import java.io.Serializable;
import java.util.List;

import top.hencter.weibo.activity.PhotoViewActivity;
import top.hencter.weibo.R;

public class IndexCardImageAdapter extends ArrayAdapter<String> {

    // 图片数组获取
    private List<String> images;

    public IndexCardImageAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
        // 图片 url 数组
        images = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.index_gridview_item, parent, false);
        final String url = getItem(position);
        ImageView imageView = view.findViewById(R.id.index_image_grid_item);
        Glide.with(getContext()).load(url).into(imageView);

        // 查看图片事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), PhotoViewActivity.class);
                // intent 放入图片数组
                intent.putExtra("currentItem", position);
                intent.putExtra("images", (Serializable) images);
                getContext().startActivity(intent);
            }
        });

        return view;
    }
}
